package com.feicheng.common.sms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Random;

@Slf4j
@Component
public class SmsUtils {

    private String appId = "1400830097";

    private String secretId = "AKIDFKBoDPN7cv33ufFetwoV2R9lFWaSAh29";

    private String secretKey = "wdIrENIyRhkbEfsshJiuteDYgbJo6B3i";

//    @Autowired
    SmsClient smsClient;

    public SmsUtils() {
        this.smsClient = this.smsClient();
    }

    private static String getCode() {
        return String.format("%04d", new Random().nextInt(9999));
    }

    public void sendMessage(JSONArray phoneNumbers, String param) throws TencentCloudSDKException {

        JSONObject smsObject = new JSONObject();
        smsObject.put("sign", "个人日志类个人网");
        smsObject.put("templateID", "1829595");
        smsObject.put("phoneNumbers", phoneNumbers);

        JSONArray templateParam = new JSONArray();
        templateParam.add(param);
        smsObject.put("templateParam", templateParam);
        sendSms(smsObject);
    }

//    @Bean
    SmsClient smsClient() {
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "",clientProfile);
        return client;
    }

    /**
     * 发送短信方法
     *
     * @param param
     * @return
     */
    private ResultInfo sendSms(JSONObject param) throws TencentCloudSDKException {

        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(appId);
        req.setSign(param.getString("sign"));
        req.setTemplateID(param.getString("templateID"));

        JSONArray phoneNumberArray = param.getJSONArray("phoneNumbers");
        String[] phoneNumbers = new String[phoneNumberArray.size()];

        for (int index = 0; index < phoneNumberArray.size(); index++) {
            Object objPhone = phoneNumberArray.get(index);
            phoneNumbers[index] = "+86"+objPhone.toString();
        }

        req.setPhoneNumberSet(phoneNumbers);
        JSONArray templateParamArray = param.getJSONArray("templateParam");

        String[] templateParams = new String[templateParamArray.size()];

        for (int jndex = 0; jndex < templateParamArray.size(); jndex++) {
            templateParams[jndex] = templateParamArray.get(jndex).toString();
        }

        req.setTemplateParamSet(templateParams);

        SendSmsResponse res = smsClient.SendSms(req);
        JSONObject result = JSONObject.parseObject(SendSmsResponse.toJsonString(res));
        log.debug(result.toJSONString());
        return ResultInfo.success(result);
    }
}
